// ======================================================================
// Copyright (C) 2013 Hell-Prototypes. / www.hellprototypes.com
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
// ======================================================================
#include "choose_cfg_file.h"
#include "ui_choose_cfg_file.h"
#include <QFileDialog>
#include <QMessageBox>

#define CFG_FILE_NUM_LIMIT      32

choose_cfg_file::choose_cfg_file(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::choose_cfg_file)
{
    ui->setupUi(this);
    setWindowFlags((windowFlags() & (~Qt::WindowContextHelpButtonHint)) | Qt::WindowStaysOnTopHint);

    m_config_file = new QSettings("Config.ini", QSettings::IniFormat);

    QStringList file_list;
    m_config_file->beginGroup("Config_Files");
    QString file_name("CFG_File_%1");
    for(int i=0; i<CFG_FILE_NUM_LIMIT; i++) {
        QVariant value = m_config_file->value(file_name.arg(i));
        if(value.canConvert(QMetaType::QString)) {
            QString string = value.toString();
            if(!string.isEmpty()) {
                file_list.push_back(string);
            }
        }
    }
    m_config_file->endGroup();

    if(file_list.size() > 0) {
        ui->lw_cfg_file_list->addItems(file_list);
    }
    ui->bb_ok_cancel->setEnabled(false);
    ui->pb_delete_file_from_disk->setEnabled(false);
    ui->pb_remove_file_from_list->setEnabled(false);
}

choose_cfg_file::~choose_cfg_file()
{
    QStringList file_list;

    for(int i=0; i<ui->lw_cfg_file_list->count(); i++) {
        QListWidgetItem *item = ui->lw_cfg_file_list->item(i);
        if(item) {
            if(!item->text().isEmpty()) {
                file_list.push_back(item->text());
            }
        }

    }

    m_config_file->beginGroup("Config_Files");
    QString file_name("CFG_File_%1");
    for(int i=0; i<CFG_FILE_NUM_LIMIT; i++) {
        if(i<file_list.size()) {
            m_config_file->setValue(file_name.arg(i), file_list.at(i));
        } else {
            if(m_config_file->contains(file_name.arg(i))) {
                m_config_file->remove(file_name.arg(i));
            }
        }
        //qDebug("%s, %s", file_name.arg(i).toLatin1().data(), file_list.at(i).toLatin1().data());
    }
    m_config_file->endGroup();

    delete m_config_file;
    delete ui;
}

void choose_cfg_file::on_bb_ok_cancel_accepted()
{
    if(ui->lw_cfg_file_list->selectedItems().size() == 1) {
        if(!ui->lw_cfg_file_list->currentItem()->text().isEmpty()) {
            m_cfg_file_path = ui->lw_cfg_file_list->currentItem()->text();
        }
    }
}

bool choose_cfg_file::add_config_file(QString file_path)
{
    if(file_path.isEmpty()) {
        return false;
    }

    QList<QListWidgetItem *> list =	ui->lw_cfg_file_list->findItems(file_path, Qt::MatchCaseSensitive);
    if(list.size() <= 0) {
        ui->lw_cfg_file_list->addItem(file_path);
    }
    return true;
}

void choose_cfg_file::on_lw_cfg_file_list_itemDoubleClicked(QListWidgetItem *item)
{
    if(!item->text().isEmpty()) {
        m_cfg_file_path = item->text();
        this->accept();
    }
}

void choose_cfg_file::on_lw_cfg_file_list_itemSelectionChanged()
{
    ui->bb_ok_cancel->setEnabled(true);
    ui->pb_delete_file_from_disk->setEnabled(true);
    ui->pb_remove_file_from_list->setEnabled(true);
}

void choose_cfg_file::on_pb_load_cfg_file_clicked()
{
    QString file_name = QFileDialog::getOpenFileName(this, tr("Load File"), "", tr("Setting (*.cfg);;All (*.*)"));

    if(file_name.isEmpty()) {
        return;
    }

    QString curr_path = QDir::currentPath();
    if(file_name.contains(curr_path)) {
        file_name.remove(curr_path + "/");
    }
    add_config_file(file_name);
}

void choose_cfg_file::on_pb_remove_file_from_list_clicked()
{
    if(ui->lw_cfg_file_list->selectedItems().size() == 1) {
        ui->lw_cfg_file_list->takeItem(ui->lw_cfg_file_list->currentRow());
    }
}

void choose_cfg_file::on_pb_delete_file_from_disk_clicked()
{
    QMessageBox::StandardButton ret;

    ret = QMessageBox::warning(this, tr("Delete warning"), "This will [Delete] select file from disk, Data will lose ...",
                                     QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
    if(ret != QMessageBox::Yes) {
        return;
    }

    QString path = ui->lw_cfg_file_list->currentItem()->text();

    QFile file(path);
    if(file.exists()) {
        file.remove();
    }

    if(ui->lw_cfg_file_list->selectedItems().size() == 1) {
        ui->lw_cfg_file_list->takeItem(ui->lw_cfg_file_list->currentRow());
    }
}

